import hashlib
from datetime import time
from random import choice
from django.contrib.auth.hashers import make_password, check_password
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from numpy import random

from blog.models import Catagory
from login import models, forms
from login.forms import UserForm
from login.models import User


def index(request):
    allcategory = Catagory.objects.all()  # 通过Category表查出所有分类
    return render(request, 'index.html', locals())


def login(request):
    allcategory = Catagory.objects.all()  # 通过Category表查出所有分类
    if request.method == "POST":
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        print(username, password)
        if username and password:  # 确保用户名和密码都不为空
            username = username.strip()
            password = password.strip()
            # 用户名字符合法性验证
            try:
                user = models.User.objects.get(name=username)
                if user.password == password:
                    request.session['is_login'] = True
                    request.session['user_id'] = user.id
                    request.session['user_name'] = user.name
                    return render(request, 'index.html', locals())
                else:
                    message = '密码不正确'
            except:
                message = '用户不存在'
        return render(request, 'login.html', locals())
    return render(request, 'login.html', locals())


def register(request):
    # if request.session.get('is_login', None):
    #     # 登录状态不允许注册。你可以修改这条原则！
    #     return redirect("/")
    if request.method == "POST":
        username = request.POST.get('username', None)
        password1 = request.POST.get('password1', None)
        password2 = request.POST.get('password2', None)
        if username and password1 and password2:  # 获取数据
            print(username, password1, password2)
            username = username.strip()
            password = password1.strip()
            password = password2.strip()
            if password1 != password2:  # 判断两次密码是否相同
                message = "两次输入的密码不同！"
                return render(request, 'register.html', locals())
            else:
                same_name_user = models.User.objects.filter(name=username)
                if same_name_user:  # 用户名唯一
                    message = '用户已经存在，请重新选择用户名！'
                    return render(request, 'register.html', locals())
                # 当一切都OK的情况下，创建新用户
                new_user = models.User()
                new_user.name = username
                new_user.password = password1
                new_user.save()
                return redirect('/login/')  # 自动跳转到登录页面
    return render(request, 'register.html', locals())


def hash_code(s, salt='dataAnalysis'):
    h = hashlib.sha256()
    s += salt
    h.update(s.encode())  # update方法只接收bytes类型
    return h.hexdigest()


def logout(request):
    if not request.session.get('is_login', None):
        return redirect('/')
    request.session.flush()
    return redirect("/login/")


# 数据分析页面
def analysis(request):
    allcategory = Catagory.objects.all()  # 通过Category表查出所有分类
    return render(request, 'dataAnalysis.html', locals())


# 帮助中心
def help(request):
    allcategory = Catagory.objects.all()  # 通过Category表查出所有分类
    return render(request, 'help.html', locals())


# 联系我们
def contact(request):
    allcategory = Catagory.objects.all()  # 通过Category表查出所有分类
    return render(request, 'contact.html', locals())
